// JSON数据源
// http://api.douban.com/v2/movie/top250
// JSON数据格式化
// http://tool.oschina.net/codeformat/json

//获取异步访问对象
var xmlhttp;
if (window.XMLHttpRequest) {
    //IE7+, FireFox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
} else {
    //IE6
    xmlhttp = new ActiveXObject("Micosoft.XMLHTTP");
}

var news_content = document.getElementById("news_content");

//HTTP是无状态，定义一些状态值描述当前HTTP的性质
//404(not found) 200(ok)  50x(服务器应用程序错误)
//定义回调函数
xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
        //news_content.innerHTML = xmlhttp.responseText;
        //JSON.parse()把文本数据解析为Json对象
        showJson(JSON.parse(xmlhttp.responseText));
    }
}

//HTTP访问方法：GET   POST  ...
// xmlhttp.open("GET", "http://api.douban.com/v2/movie/top250", true);
xmlhttp.open("GET", "top250.json", true);
xmlhttp.send();

function showJson(jsonData) {
    document.getElementById("title").innerText = jsonData.title;
    var tempHtml = "";
    for (var i = 0; i < 10; i++) {
        // tempHtml += "<li>" + jsonData.subjects[i].title + "</li>\n";
        tempHtml += "<li>";
        tempHtml += jsonData.subjects[i].title;
        tempHtml += "<img src=\"";
        tempHtml += jsonData.subjects[i].images.medium;
        tempHtml += "\" alt=\"";
        tempHtml += jsonData.subjects[i].title;
        tempHtml += "\">";
        tempHtml += "</li>";
    }

    document.getElementById("news_list").innerHTML = tempHtml;
}